package com.hskn.hss.module.button.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.button.entity.SysButton;
import com.hskn.hss.module.button.mapper.SysButtonMapper;
import com.hskn.hss.module.button.service.ISysButtonService;
import com.hskn.hss.utils.GroupingByUtils;
import com.hskn.hss.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-04
 */
@Service
public class SysButtonServiceImpl extends ServiceImpl<SysButtonMapper, SysButton> implements ISysButtonService {
    @Autowired
    SysButtonMapper sysButtonMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {
        SysButton sysButton = params.getObject("data", SysButton.class);
        sysButton.setId(StringUtils.uuid());
        int i = sysButtonMapper.insert(sysButton);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult deleteByid(JSONObject params) throws HssException {
        String id = params.getString("id");
        SysButton sysButton = sysButtonMapper.selectById(id);
        int i = sysButtonMapper.updateById(sysButton);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {
        SysButton sysButton = params.getObject("data", SysButton.class);
        int i = sysButtonMapper.updateById(sysButton);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public SysButton getInfoById(JSONObject params) throws HssException {
        String id = params.getString("id");
        SysButton sysButton = sysButtonMapper.selectById(id);
        return sysButton;
    }

    @Override
    public JSONObject getList(JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map map = new HashMap();
        map.put("menuid", params.getString("menuid"));
        map.put("name", params.getString("name"));
        map.put("type", params.getString("type"));
        map.put("status", params.getString("status"));


//        QueryWrapper<SysButton> queryWrapper = new QueryWrapper<>();
//        queryWrapper.allEq(map,false);
//        queryWrapper.orderByDesc("menuid");
//        IPage<SysButton> iPage = sysButtonMapper.selectPage(btnPage, queryWrapper);

        Page<SysButton> btnPage = new Page<>(page, size, true);
        IPage<SysButton> iPage = sysButtonMapper.listButton(btnPage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("btnlist", iPage.getRecords());
        return json;
    }

    @Override
    public List<SysButton> GetButtonListByMenuId(String menuid) {
        List<SysButton> buttonList = sysButtonMapper.GetButtonListByMenuId(menuid);
        return buttonList;
    }
    @Override
    public List<SysButton> selectList(Wrapper<SysButton> sysButtonWrapper) {
        List<SysButton> buttonList = sysButtonMapper.selectList(sysButtonWrapper);
        return buttonList;
    }
    @Override
    public Map<String,List<SysButton>> getListMap(Wrapper<SysButton> sysButtonWrapper, Function<? super SysButton, ? extends String> classifier) {
        List<SysButton> buttonList = sysButtonMapper.selectList(sysButtonWrapper);
        Map<String,List<SysButton>> result = new HashMap<>();
        if(CollectionUtils.isNotEmpty(buttonList)){
            result = GroupingByUtils.getGroupingForMapList(buttonList,classifier);
        }
        return result;
    }
}
